import store from '@/store'
import { createRouter, createWebHistory } from 'vue-router'
import axios from "axios";
import {ElMessage} from "element-plus";
const routes = [
  {
    path: '/dologin',
    name: 'login',
    component: () => import('@/components/LeEn_admin/Login')
  },

  {
    path: '/login',
    name: '登录',
    component: () => import('@/components/User/login')
  }, {
    path: '/searchRecords',
    name: '搜题记录',
    component: () => import('@/components/User/searchRecords')
  }
  , {
    path: '/courseIndex',
    name: '课程首页',
    component: () => import('@/components/course/courseIndex')
  },{
    path: '/test',
    name: '搜题记录',
    component: () => import('@/components/course/test')
  }

  ,
  {
    path:'/',
    name:'首页',
    component: () => import('@/components/BackgroundManagement/HomePage'),
    redirect:'/homePageIndex',
    children:[
      {
        path:'/questionBank',
        name:'题库',
        component:() => import('@/components/LeEn_questionBank/main'),
        meta:{title:'首页'}
      },
      {
        path:'/userManagement',
        name:'用户管理页面',
        component:() => import('@/components/User/UserManagement')
        ,meta:{title:'用户管理页面'}
      },
      {

        path:'/phonetest',
        name:'测试短信登录',
        component:() => import('@/components/User/PhoneTest')        
      },
      {
        path:"/permission",
        name:"权限管理页面",
        component:()=>import('@/components/LeEn_admin/Permission')
        ,meta:{title:'权限管理页面'}
      },
      {
        path:"/userRegistration",
        name:"用户注册表",
        component:()=>import('@/components/LeEn_admin/statements/UserRegistration')
      },
      {
        path:"/homePageIndex",
        name:"后台首页",
        component: () => import('@/components/BackgroundManagement/HomePageIndex'),
      },
      {
        path:"/courseList",
        name:"课程列表",
        component: () => import('@/components/course/courseList'),

        meta:{title:'课程列表'}

      },
      {
        path:"/courseTagList",
        name:"课程标签列表",
        component: () => import('@/components/course/courseTagList'),

        meta:{title:'课程列表'}

      },
      {
        path:"/courseNotes",
        name:"笔记管理",
        component: () => import('@/components/course/courseNotes'),
        meta:{title:'笔记管理'}
      },
      {
        path:"/courseCatalog",
        name:"视频管理",
        component: () => import('@/components/course/courseCatalog'),
        meta:{title:'视频管理'}
      },
      {
        path:'/question',
        name:'问题管理',
        component:() => import('@/components/mao_question/que1/Addye')
      },
      {
        path:'/question2',
        name:'问题分类管理',
        component:() => import('@/components/mao_question/que1/TableList')
      },
      {
        path:"/financial",
        name:"订单管理",
        component:()=> import('@/components/LeEn_admin/FinancialStatementAnalysis')

      },
      {
        path:'/questionBank/industry',
        name:'科目行业',
        component:() => import('@/components/LeEn_questionBank/main'),
        meta:{title:'科目管理'}
      },
      {
        path:'/questionBank/subject',
        name:'题目管理',
        component:() => import('@/components/LeEn_questionBank/topic/main'),
        meta:{title:'题管理'}
      },
      {
        path:'/statisDatas',
        name:'报表统计',
        component:() => import('@/components/BackgroundManagement/Statistics'),
        meta:{title:'报表统计'}
      },
      {
        path: '/404',
        name: '404',
        component: () => import('@/components/errorPrompt')
      },
      {
        path:"/orderRegistration",
        name:"订单月报表",
        component:()=>import('@/components/LeEn_admin/statements/OrderRegistration')
      },
      {
        path:"/403",
        name:"403",
        component:()=>import('@/components/LeEn_admin/page/403')
      },
      {
        path:'/questionBank/test_paper',
        name:'试卷管理',
        component:() => import('@/components/LeEn_questionBank/test_paper/main'),
        meta:{title:'题管理'}
      }
      ,{
        path:'/question',
        name:'问题管理',
        component:() => import('@/components/mao_question/Question'),
        meta:{title:'问题管理'}
      },{
        path:'/questionCategory',
        name:'问题分类管理',
        component:() => import('@/components/mao_question/QuestionCategory'),
        meta:{title:'问题分类管理'}
      },{
        path:'/chart',
        name:'问题趋势总览',
        component:() => import('@/components/mao_question/Chart1'),
        meta:{title:'问题趋势总览'}
      }
      // ,{
      //   path:'/ask',
      //   name:'我要提问',
      //   component:() => import('@/components/mao_question/Ask'),
      //   meta:{title:'我要提问'}
      // }
    ]
    
  },
  
 

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 导航守卫 ,路由守卫,路由拦截器
router.beforeEach((to,from,next) =>{
    //验证token,只有存在token 时,才能跳转到内容页
  // console.log(to)
    let token = store.state.token
    if(token || to.path == "/dologin" || to.path == "/login" || to.path=="/api/verification"){
      // console.log("qian")
      next();
    }else{
      // console.log("hou")
      next("/dologin");
    }
})
axios.interceptors.response.use(function (response){
  // console.log("response:"+response)
  return response;
}, function (error){
  // console.log("error:"+error)
  if(error.response.data.message=='600'){
    // ElMessage.error("Token过期")
    localStorage.setItem("token", null);
    localStorage.clear();
    store.commit("clearuser");
    location.href = "/dologin";
  }
  if(error.response.data.code==400){
    router.replace({ path: "/403" });
  }
  if(error.response.status == 404){
    localStorage.setItem("token", null);
    localStorage.clear();
    store.commit("clearuser");
    console.log(store.state.user);
    location.href = "/dologin";
  }
})
export default router
